package java6681.lesson02;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.*;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;

import java.util.Arrays;

/**
 * @author Junichiro
 * @description 字符串中的数字排序
 * @date 2021-03-14 22:12
 **/

public class StringSort6681 extends Application {
    public static void main(String[] args) {
       launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Scene scene = new Scene(getRoot());
        primaryStage.setScene(scene);
        primaryStage.setTitle("字符串中的数字排序");
        primaryStage.show();
    }
    private Parent getRoot(){
        VBox root = new VBox(20);
        Label lblStudent = new Label("212006681|张舒婷");
        /*边框设置*/
        BorderStroke bs = new BorderStroke(Paint.valueOf("#53868B")//边框的颜色
                , BorderStrokeStyle.DOTTED//边框的样式
                ,new CornerRadii(10)//边框四个角的圆角
                ,new BorderWidths(3));//边框的宽度
        /*设置边框*/
        lblStudent.setBorder(new Border(bs));
        /*设置边距*/
        lblStudent.setPadding(new Insets(10));

        /*水平排列两个控件，用于输入*/
        HBox hText = new HBox(10);
        Label lblText = new Label("输入数字串：");
        TextField tfText = new TextField();
        /*文本框内无内容的时候显示的信息*/
        tfText.setPromptText("用空格分割的数字串");
        hText.getChildren().addAll(lblText,tfText);
        /*两个控件的对齐方式*/
        hText.setAlignment(Pos.CENTER_LEFT);

        Button button = new Button("排序");
        /*显示排序结果*/
        Label lblPrint = new Label();

        root.getChildren().addAll(lblStudent,hText,button,lblPrint);
        /*设置根节点的边距*/
        root.setPadding(new Insets(20.0));
        /*根节点中各节点的对齐方式*/
        root.setAlignment(Pos.TOP_CENTER);
        /*用CSS样式设置容器中所有节点的字体大小*/
        root.setStyle("-fx-font-size:20");
        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                /*录入的字符串*/
                String str = tfText.getText();
                /*分割字符串，得到字符串数组*/
                String[] split = str.split(" ");
                /*创建int数组以分割的数组字符串为长度*/
                int[] arr = new int[split.length];
                /*将分割好的字符遍历存入int数组中*/
                for (int i = 0; i < arr.length; i++) {
                    arr[i] = Integer.parseInt(split[i]);
                }
                /*对int数组进行排序*/
                Arrays.sort(arr);
                /*排序后的int数组组成一个字符串*/
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < arr.length; i++) {
                    sb.append(arr[i]).append(" ");
                }
                /*转换为String*/
                String result = sb.toString().trim();
                /*322 315 66 222 77 101*/
                lblPrint.setText(result);
            }
        });
        return root;
    }
}